//app.js
App({
  data: {
    appid: 'wx1c35321f771c0c31',
    secret: '87a61920c73a6a73ed11cd65d8c74469',

    server: 'http://172.30.16.133/',
    versionId: 'SJAPP1000000000000003WECHAT01',
    url:{
      getHomeTypeList: '/api/list/getHomeTypeList',//查询-首页分类及二级分类
      getAppShopList: 'api/List/getAppShopList',
      getSearchKeyList: 'api/list/getSearchKeyList',
      getMemAccInfo: 'api/user/getMemberMoneyList',
      GetToken: 'api/oauth/GetToken'//获取token
    }
  },
  onLaunch: function () {
    //同步清理本地数据缓存
    wx.clearStorageSync();

    // 获取token
    //this.initToken();

    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  globalData: {
    userInfo: null
  },

  /**
   * 初始化token
   */
  initToken: function(callback) {
    let that = this;
    wx.getUserInfo({
      withCredentials: true,
      success: function(res){
        console.log('========='+JSON.stringify(res));
      }
    })
    return;



    //获取code
    wx.login({
      success: function (res) {
        console.log('获取code值:' + res.code);
        //获取openid
        wx.request({
          url: 'https://api.weixin.qq.com/sns/jscode2session',
          data: {
            'appid': that.data.appid,
            'secret': that.data.secret,
            'js_code': res.code,
            'grant_type': 'authorization_code'
          },
          method: 'GET',
          success: function (res) {
            console.log('获取openid值:'+JSON.stringify(res.data));
            //获取token
            wx.request({
              url: that.data.server + that.data.url.GetToken,
              method: 'POST',
              header: {
                'Content-Type': 'application/json'
              },
              data: {
                "ApplicationId": "SJAPP1000000000000003",
                "DeviceInfo": {
                  "AppVersion": "SJAPP1000000000000003WECHAT01",
                  "DeviceCpuArchitecture": "x86",
                  "DeviceId": "openid" + res.data.openid,
                  "DeviceName": "苹果",
                  "DeviceType": "米4",
                  "Ip": "100.200.100.100",
                  "Mac": "00-01-6C-06-A6-29",
                  "OsName": "安卓",
                  "OsVersion": "android6.0",
                  "PushId": "1erdg432dfdsrs45w4sdf54s5df5",
                  "RomVersion": "MUI6"
                }
              },
              dataType: 'json',
              success: function (res) {
                if (res.data.Successed) {
                  wx.setStorageSync('localToken', res.data.Data.Token)
                  console.log('token:' + res.data.Data.Token)
                  callback();
                } else {
                  console.log('获取token失败！原因：' + res.data.Message)
                }
              }
            })

          }
        })
      }
    })
  },
  /**
   * 获取本地token
   */
  getToken: function() {
    return wx.getStorageSync('localToken');
  }

})